
package com.yunfei.api.smrz;

import com.yunfei.api.smrz.bo.AppFileSimpleBo;
import com.yunfei.api.smrz.bo.ShenHeYiJianBo;
import com.yunfei.bo.hgtzz.HeGeTouZiZheRenZhengBo;
import com.yunfei.bo.hgtzz.HeGeTouZiZheRenZhengSaveParam;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.bo.smrz.IdCardBo;
import com.yunfei.bo.smrz.IdCardType;
import com.yunfei.bo.smrz.ZhuanYeTouZiZheRenZhengBo;
import com.yunfei.bo.zytzz.ZhuanYeTouZiZheRenZhengResultBo;
import com.yunfei.bo.zytzz.ZhuanYeTouZiZheRenZhengSaveParam;
import com.yunfei.enums.ResultStatus;
import com.yunfei.enums.ShiMingRenZhengStatus;
import com.yunfei.mapper.aiiidb.*;
import com.yunfei.mapper.crm.ShiMingRenZhengFileMapper;
import com.yunfei.mapper.crm.TblobtSorageMapper;
import com.yunfei.mapper.crm.ZhuanYeTouZiZheRenZhengFileMapper;
import com.yunfei.service.ShiMingRenZhengService;
import com.yunfei.utils.TokenUtil;
import com.yunfei.utils.json.JsonListUtil;
import com.yunfei.utils.procedure.ShiMingRenZhengProcedureReturnInfo;
import com.yunfei.utils.procedure.ZhuanYeTouZiZheRenZhengProcedure;
import com.yunfei.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 专业投资者认证接口
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@Api(value = "专业投资者认证",tags = "专业投资者认证(理财师发起)")
@RestController
@RequestMapping("/api/licaishi/zytzz")
public class ZhuanYeTouZiZheRenZhengApiController {
    @Autowired
    private ShiMingRenZhengMapper shiMingRenZhengMapper;
    @Autowired
    private ShenHeYiJianMapper shenHeYiJianMapper;
    @Autowired
    private ShiMingRenZhengService shiMingRenZhengService;
    @Autowired
    private ZhuanYeTouZiZheRenZhengMapper zhuanYeTouZiZheRenZhengMapper;
    @Autowired
    private HeGeTouZiZheRenZhengMapper heGeTouZiZheRenZhengMapper;
    @Autowired
    private Environment env;
    @Autowired
    private TblobtSorageMapper tblobtSorageMapper;
    @Autowired
    private ZhuanYeTouZiZheRenZhengFileMapper zhuanYeTouZiZheRenZhengFileMapper;
    @Autowired
    private CrmAcceDtlMapper crmAcceDtlMapper;
    @Autowired
    private ShiMingRenZhengFileMapper shiMingRenZhengFileMapper;

    //页面初始化
    @GetMapping("/get")
    @ApiOperation(value = "获取所有证件类型",notes = "获取所有证件类型",position = 1,response = Object.class)
    public Object getAllCardType(String cCustNo){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        List<IdCardType> idCardTypeList=shiMingRenZhengMapper.getCardType(null);//证件类型
        List<IdCardType> zhengMingType=heGeTouZiZheRenZhengMapper.getZhengMingType(null);//证明类型
        resultMap.put("cardType",idCardTypeList);
        resultMap.put("zhengMingType",zhengMingType);
        Map<String,Object> queryMap=new HashMap<>();
        queryMap.put("cCustNo",cCustNo);
        IdCardBo cardBo= shiMingRenZhengMapper.getLookInfo(queryMap);
        resultMap.put("userInfo",cardBo);
        resultModel.setCode(ResultStatus.SUCCESS.getCode());
        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        resultModel.setData(resultMap);
        return resultModel;
    }
    //查询列表
/*    @GetMapping("/list")
    @ApiOperation(value = "查询列表",notes = "查询列表",position = 1,response = Object.class)
    public Object getList(String token){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> queryMap=new HashMap<>();
        String cCustNo= TokenUtil.getInstance().getUserId(token);
        queryMap.put("cCustNo",cCustNo);
        List<HeGeTouZiZheRenZhengBo> resultList=heGeTouZiZheRenZhengMapper.getList(queryMap);//查询列表
        resultModel.setCode(ResultStatus.SUCCESS.getCode());
        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        resultModel.setData(resultList);
        return resultModel;
    }*/
    //查询
    @GetMapping("/edite")
    @ApiOperation(value = "查看",notes = "查看",position = 1,response = Object.class)
    public Object edite(String cCustNo){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> queryMap=new HashMap<>();
        queryMap.put("cCustNo",cCustNo);
        String lSerialno=zhuanYeTouZiZheRenZhengMapper.getId(queryMap);
        if(StringUtil.isNullOrEmpty(lSerialno)){
            lSerialno="";
        }
        List<AppFileSimpleBo> fileList=zhuanYeTouZiZheRenZhengMapper.getFileList(queryMap);
        resultMap.put("resultFile",fileList);
        queryMap.put("lSerialno",lSerialno);
       //HeGeTouZiZheRenZhengBo result=zhuanYeTouZiZheRenZhengMapper.edite(queryMap);//获取单个值
       // resultMap.put("userInfo",result);
       Map<String,Object> userInfoMap=new HashMap<>();
        userInfoMap.put("cCustNo",cCustNo);
        IdCardBo cardBo= shiMingRenZhengMapper.getLookInfo(userInfoMap);
        HeGeTouZiZheRenZhengBo userInfo=new HeGeTouZiZheRenZhengBo();
        userInfo.setCardNo(cardBo.getCardNo());
        userInfo.setCardType(cardBo.getCardType());
        userInfo.setCustNo(cardBo.getcCustNo());
        userInfo.setUserName(cardBo.getUserName());
        userInfo.setMobile(cardBo.getMobile());
        userInfo.setlSerialNo(lSerialno);
        ZhuanYeTouZiZheRenZhengResultBo zhuanYeTouZiZheRenZhengBo=zhuanYeTouZiZheRenZhengMapper.getRenZhengInfo(queryMap);
       if(!StringUtil.isNullOrEmpty(zhuanYeTouZiZheRenZhengBo)){
           userInfo.setcStatus(zhuanYeTouZiZheRenZhengBo.getcStatus());//审核状态，中文
           userInfo.setZhengMingType(zhuanYeTouZiZheRenZhengBo.getZhengMingType());//证明类型
           userInfo.setMoney(zhuanYeTouZiZheRenZhengBo.getMoney());//证明金额
       }

        resultMap.put("userInfo",userInfo);
        //获取审核意见
        Map<String,Object> shenHeQueryMap=new HashMap<>();
        if(StringUtil.isNullOrEmpty(lSerialno)){
            lSerialno="";
        }
        shenHeQueryMap.put("lSerialno",lSerialno);
        List<ShenHeYiJianBo> shenHeYiJianBoList=shenHeYiJianMapper.zytzzShenHeYiJianList(shenHeQueryMap);
        resultMap.put("shenHeYiJian",shenHeYiJianBoList);
        List<IdCardType> idCardTypeList=shiMingRenZhengMapper.getCardType(null);//证件类型
        List<IdCardType> zhengMingType=heGeTouZiZheRenZhengMapper.getZhengMingType(null);//证明类型
        resultMap.put("cardType",idCardTypeList);
        resultMap.put("zhengMingType",zhengMingType);
        resultModel.setCode(ResultStatus.SUCCESS.getCode());
        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        resultModel.setData(resultMap);
        return resultModel;
    }
    //录入
    @PostMapping(value = "/save",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "录入",notes = "合格投资者认证(理财师发起)",position = 1,response = Object.class)
    @ResponseBody
    public Object save(HeGeTouZiZheRenZhengSaveParam param, HttpServletRequest request){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        //参数格式校验
        String cCustNo=param.getCustNo();//客户编号
        String zhengMingType=param.getZhengMingType();//证明类型
        String money=param.getMoney();//证明金额
        if(StringUtil.isNullOrEmpty(zhengMingType)){
            resultModel.setCode(ShiMingRenZhengStatus.ZHENGMINGLEIXING_NOTNULL_ERRPR.getCode());
            resultModel.setMessage(ShiMingRenZhengStatus.ZHENGMINGLEIXING_NOTNULL_ERRPR.getMessage());
            return resultModel;
        }
        if(StringUtil.isNullOrEmpty(money)){
            resultModel.setCode(ShiMingRenZhengStatus.ZHENGMINGMONEY_NOTNULL_ERRPR.getCode());
            resultModel.setMessage(ShiMingRenZhengStatus.ZHENGMINGMONEY_NOTNULL_ERRPR.getMessage());
            return resultModel;
        }
        //校验附件必填
        String imgLength=param.getImgLength();
        if(!StringUtil.isNullOrEmpty(imgLength)){
            int count=Integer.parseInt(imgLength);
            if(count==0){
                resultModel.setMessage(ShiMingRenZhengStatus.ZHENGMINGZILIAO_PIC_ERROR.getMessage());
                resultModel.setCode(ShiMingRenZhengStatus.ZHENGMINGZILIAO_PIC_ERROR.getCode());
                return resultModel;
            }
        }else{
            resultModel.setMessage(ShiMingRenZhengStatus.ZHENGMINGZILIAO_PIC_ERROR.getMessage());
            resultModel.setCode(ShiMingRenZhengStatus.ZHENGMINGZILIAO_PIC_ERROR.getCode());
            return resultModel;
        }
        ZhuanYeTouZiZheRenZhengBo zhuanYeTouZiZheRenZhengBo=new ZhuanYeTouZiZheRenZhengBo();
        zhuanYeTouZiZheRenZhengBo.setpCustno(cCustNo);
        zhuanYeTouZiZheRenZhengBo.setpProvetype(zhengMingType);
        zhuanYeTouZiZheRenZhengBo.setpProvebalance(money);
        Connection con =shiMingRenZhengService.getConnection();
        ZhuanYeTouZiZheRenZhengProcedure zhuanYeTouZiZheRenZhengProcedure=new ZhuanYeTouZiZheRenZhengProcedure();
        ShiMingRenZhengProcedureReturnInfo returnInfo= zhuanYeTouZiZheRenZhengProcedure.save(zhuanYeTouZiZheRenZhengBo,con);
        // shiMingRenZhengService.updateSmrzStatus(resultModel, cCustNo,userName);
        int count=zhuanYeTouZiZheRenZhengMapper.isSave(zhuanYeTouZiZheRenZhengBo);
        if(count==0){
            zhuanYeTouZiZheRenZhengMapper.saveZhuanYeRenZhengInfo(zhuanYeTouZiZheRenZhengBo);
        }
        String reasonCode=returnInfo.getpReasonCode();//返回结果编码0失败1成功
        String reasonInfo=returnInfo.getpReasonInfo();//返回成功时未附件输出编号，返回失败时为错误提示信息
        queryMap.put("smrzResult",returnInfo);
        if("1".equals(reasonCode)){
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            resultModel.setData(reasonInfo);
        }else{
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(reasonInfo);
        }
        return resultModel;
    }
    //编辑保存
    //录入
    @PostMapping(value = "/update",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "编辑保存",notes = "合格投资者认证(理财师发起)",position = 1,response = Object.class)
    @ResponseBody
    public Object update(ZhuanYeTouZiZheRenZhengSaveParam param, HttpServletRequest request,String imagJson){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        System.out.println("imgJson=========="+imagJson);
        List<AppFileSimpleBo> imgList= JsonListUtil.jsonToList(imagJson,AppFileSimpleBo.class);
        List<AppFileSimpleBo> notDeleteImage=new ArrayList<>();
        //参数格式校验
        String cCustNo=param.getCustNo();//客户编号
        String zhengMingType=param.getZhengMingType();//证明类型
        String money=param.getMoney();//证明金额
        queryMap.put("cCustNo",cCustNo);
        String id= zhuanYeTouZiZheRenZhengMapper.getId(queryMap);
        if(StringUtil.isNullOrEmpty(id)){
            id="";
        }
       // param.setlSerialno(id);
        zhuanYeTouZiZheRenZhengMapper.update(param);
       /* Map<String,Object> deleteParam=new HashMap<>();
        deleteParam.put("lSerialno",id);
        zhuanYeTouZiZheRenZhengMapper.deleteOldFile(deleteParam);*/
        try{
            //处理附件
            for(int i=0;i<imgList.size();i++){
                boolean isDelete=imgList.get(i).isDelete();
                String cCatalog=imgList.get(i).getcCatalog();
                if(isDelete){
                    //删除app附件
                    Map<String,Object> acceDtlMap=new HashMap<>();
                    acceDtlMap.put("lStorageid",imgList.get(i).getlStorageId());
                    //删除app附件
                    crmAcceDtlMapper.deleteByMap(acceDtlMap);
                    crmAcceDtlMapper.deleteOldTblobStorageFileByMap(acceDtlMap);
                    //删除crm附件
                    shiMingRenZhengFileMapper.deleteOldFileByMap(acceDtlMap);
                }
            }
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){

        }
        //设置cSubkeyId
        Map<String,Object> querySubkeyIdMap=new HashMap<>();
        querySubkeyIdMap.put("cCustNo",cCustNo);
        String cSubkeyId=zhuanYeTouZiZheRenZhengMapper.getcSubKeyId(querySubkeyIdMap);
        resultModel.setData(cSubkeyId);
        return resultModel;
    }

    //查询
    @GetMapping("/getCrmFileList")
    @ApiOperation(value = "获取crm附件",notes = "获取crm附件",position = 1,response = Object.class)
    public Object getCrmFileList(String cCustNo){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> queryMap=new HashMap<>();
        queryMap.put("cCustNo",cCustNo);
        String lSerialno=zhuanYeTouZiZheRenZhengMapper.getId(queryMap);
        if(StringUtil.isNullOrEmpty(lSerialno)){
            lSerialno="";
        }
        List<AppFileSimpleBo> fileList=zhuanYeTouZiZheRenZhengMapper.getCrmFileList(queryMap);
        resultMap.put("resultFile",fileList);
        resultModel.setCode(ResultStatus.SUCCESS.getCode());
        resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        resultModel.setData(resultMap);
        return resultModel;
    }
}
